package com.onlyphoto.onlymall.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import com.onlyphoto.onlymall.interfaces.RetryCallback
import com.onlyphoto.onlymall.widget.CommonStatusView
import com.onlyphoto.onlymall.widget.DefaultLoadingDialog
import com.trello.rxlifecycle4.components.support.RxFragment
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager

/**
 * Created by gaochujia on 2019-6-27.
 */
abstract class BaseFragment<T : BasePresenter<*, *>> : RxFragment(), RetryCallback {
    var defaultPostLoadingDialog: DefaultLoadingDialog? = null
    lateinit var mContext: Context
    var mPresenter: T? = null
    protected var statusLayoutManager: StatusLayoutManager? = null
    protected var builder: StatusLayoutManager.Builder? = null
    protected var commonStatusView: CommonStatusView? = null
    var mRootView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        if (mRootView == null) {
            if (getLayoutRes() != 0) {
                mRootView = LayoutInflater.from(mContext)
                        .inflate(getLayoutRes(), container, false)
                defaultPostLoadingDialog = DefaultLoadingDialog()
                defaultPostLoadingDialog!!.isCancelable = false
                commonStatusView = CommonStatusView(activity)
                createPresenter()

            } else {
                throw RuntimeException("layoutResID==-1 have u create your layout?")
            }
        }

        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    protected fun showDefaultDialog() {
        if (null != defaultPostLoadingDialog!!.dialog && defaultPostLoadingDialog!!.dialog!!.isShowing) {
            return
        }
        defaultPostLoadingDialog?.show(requireActivity().supportFragmentManager, "loading", true)
    }

    protected fun hideDefaultDialog() {
        if (null != defaultPostLoadingDialog) {
            if (null != defaultPostLoadingDialog!!.dialog && defaultPostLoadingDialog!!.dialog!!.isShowing) {
                defaultPostLoadingDialog!!.dismissAllowingStateLoss()
            }
        }
    }

    /**
     * 获得全局的，防止使用getActivity()为空
     *
     * @param context
     */
    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
    }

    /**
     * 创建presenter实例
     */
    abstract fun createPresenter()


    /**
     * 初始化
     */
    protected abstract fun init()

    /**
     * 传入布局文件
     *
     * @return
     */
    abstract fun getLayoutRes(): Int

    override fun onDestroy() {
        super.onDestroy()
        mPresenter?.onDestroy()//页面销毁网络请求也取消
        if (null != defaultPostLoadingDialog!!.dialog && defaultPostLoadingDialog!!.dialog!!.isShowing) {
            defaultPostLoadingDialog!!.dismissAllowingStateLoss()
        }
    }

    override fun onPause() {
        super.onPause()
        if (null != defaultPostLoadingDialog!!.dialog && defaultPostLoadingDialog!!.dialog!!.isShowing) {
            defaultPostLoadingDialog!!.dismissAllowingStateLoss()
        }
    }

    protected fun showCustomLayout(customLayout: View, @IdRes vararg clickViewID: Int) {
        if (null == statusLayoutManager) {
            throw RuntimeException("you may forget init StatusLayoutManager first?")
        }
        if (null != statusLayoutManager && null != defaultPostLoadingDialog) {
            if (null != defaultPostLoadingDialog!!.dialog && defaultPostLoadingDialog!!.dialog!!.isShowing) {
                defaultPostLoadingDialog!!.dismissAllowingStateLoss()
            }
            statusLayoutManager!!.showCustomLayout(customLayout, *clickViewID)
        }
    }

    protected fun showSuccess() {
        if (null == statusLayoutManager) {
            throw RuntimeException("you may forget init StatusLayoutManager first?")
        }
        if (null != statusLayoutManager && null != defaultPostLoadingDialog) {
            if (null != defaultPostLoadingDialog!!.dialog && defaultPostLoadingDialog!!.dialog!!.isShowing) {
                defaultPostLoadingDialog!!.dismissAllowingStateLoss()
            }
            statusLayoutManager!!.showSuccessLayout()
        }
    }
}